package com.example.demothree.controller;

import com.example.demothree.bean.Stock;
import com.example.demothree.service.SkService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("sk")
public class SkController {
    @Resource
    private SkService skService;
    private Stock stock;

    @RequestMapping("sksprk")
    public String sksprk(HttpServletRequest req, Model model,
                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                         @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<Stock> pagesprk = skService.skspck(page,size);
        model.addAttribute("pagesprk",pagesprk);
        return "sksprk";
    }
    //商品入库
    @RequestMapping("sprkOne")
    public String sprkOne(Model model,HttpServletRequest request) {
        Long kcId = Long.valueOf(request.getParameter("kcId"));
        Long count = Long.valueOf(request.getParameter("count"));
        System.out.println(kcId);
        stock = skService.findkcykBy(kcId);
        stock.setCounts(stock.getCounts()+count);
        System.out.println(stock.getArea());
        skService.addkc(stock);
        return "forward:/sk/sksprk";
    }
    @RequestMapping("skspck")
    public String skspck(HttpServletRequest req, Model model,
                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                         @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<Stock> pagespck = skService.skspck(page,size);
        model.addAttribute("pagespck",pagespck);
        return "skspck";
    }

    //商品出库
    @RequestMapping("spckOne")
    public String spckOne(Model model,HttpServletRequest request) {
        Long kcId = Long.valueOf(request.getParameter("kcId"));
        Long count = Long.valueOf(request.getParameter("count"));
        System.out.println(kcId);
        stock = skService.findkcykBy(kcId);
        stock.setCounts(stock.getCounts()-count);
        System.out.println(stock.getArea());
        skService.addkc(stock);
        return "forward:/sk/skspck";
    }
//    @RequestMapping("skkccx")
//    public String skkccx() {
//        return "skkccx";
//    }
    @RequestMapping("skkccx")
    public String skkccx(Model model,HttpServletRequest request) {
        String spId = request.getParameter("spId");
        if (spId != null) {
            stock = skService.findspidBy(Long.valueOf(spId));
            model.addAttribute("stock", stock);
        }
        return "skkccx";
    }
    @RequestMapping("skspyk")
    public String skspyk(HttpServletRequest req, Model model,
                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                         @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<Stock> pagespyk = skService.skspck(page,size);
        model.addAttribute("pagespyk",pagespyk);
        return "skspyk";
    }
    @RequestMapping("spykOne")
    public String spykOne(Model model,HttpServletRequest request) {
        Long kcId = Long.valueOf(request.getParameter("kcId"));
        String area = request.getParameter("area");
        System.out.println(kcId);
        stock = skService.findkcykBy(kcId);
        stock.setArea(area);
        System.out.println(stock.getArea());
        skService.addkc(stock);
        return "forward:/sk/skspyk";
    }
    @RequestMapping("skkcpd")
    public String skkcpd(HttpServletRequest req, Model model,
                         @RequestParam(value = "page", defaultValue = "0") Integer page,
                         @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<Stock> pagekcpd = skService.skspck(page,size);
        model.addAttribute("pagekcpd",pagekcpd);
        return "skkcpd";
    }
}
